package com.hkts.naturalgas.utils;

import com.google.gson.Gson;
import com.hkts.naturalgas.association.service.IHktsRequestAddressAssociationService;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionDictionariesService;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionItemService;
import com.hkts.naturalgas.dictionaries.utils.DBUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassNameHttpDataScheduling
 * @Description
 * @Author yck
 * @Date2021-10-10 10:29
 * @Version V1.0
 **/
@Component
@Slf4j
public class HttpDataScheduling {

    @Autowired
    public IHktsEnvironmentProtectionDictionariesService iHktsEnvironmentProtectionDictionariesService;
    @Resource
    private HttpAPIService httpAPIService;
    @Autowired
    public DBUtils dBUtils;
    @Autowired
    private IHktsRequestAddressAssociationService iHktsRequestAddressAssociationService;


    @Autowired
    public IHktsEnvironmentProtectionItemService iHktsEnvironmentProtectionItemService;

    public Map<String, Object> HttpData() throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("请求时间：{}", format.format(new Date()));
        //TODO 判断当前的运行状态
        Map<String, Object> result = new HashMap<String, Object>();
        String s = httpAPIService.doGet("http://192.168.99.67:5000//biz");
        Gson gson = new Gson();
        Map map = new HashMap();
        map = gson.fromJson(s, map.getClass());
        Map data = (Map) map.get("data");
        result.put("dsdx_1lc", getValue(data.get("ifix.scada.SCADA.AI.DS_DX_YLPS_YI_CANG_LW_.F_CV")));
        result.put("dsdx_2lc", getValue(data.get("ifix.scada.SCADA.AI.DS_DX_YLPS_ER_CANG_LW.F_CV")));
        result.put("dsdx_3lc", getValue(data.get("ifix.scada.SCADA.AI.DS_DX_YLPS_SAN_CANG_LW_.F_CV")));
        result.put("dsdx_4lc", getValue(data.get("ifix.scada.SCADA.AI.DS_DX_YLPS_SI_CANG_LW_.F_CV")));
        result.put("dsxx_1lc", getValue(data.get("ifix.scada.SCADA.AI.DS_XX_YLPS_YI_CANG_LW.F_CV")));
        result.put("dsxx_2lc", getValue(data.get("ifix.scada.SCADA.AI.DS_XX_YLPS_ER_CANG_LW.F_CV")));
        result.put("dsxx_3lc", getValue(data.get("ifix.scada.SCADA.AI.DS_XX_YLPS_SAN_CANG_LW.F_CV")));
        result.put("dsxx_4lc", getValue(data.get("ifix.scada.SCADA.AI.DS_XX_YLPS_SI_CANG_LW.F_CV")));
        result.put("ds_1sd_2v", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD2_ADONG_CI_DIAN_YA.F_CV")));
        result.put("ds_1sd_2i", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD2_YI_DIAN_CD_CI_DL.F_CV")));
        result.put("ds_2sd_2v", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD3_BDONG_CI_DIAN_YA.F_CV")));
        result.put("ds_2sd_2i", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD3_DONG_DIAN_CD_CI_DL.F_CV")));
        result.put("dsjh_tlph", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_TL_PH_101.F_CV")));
        result.put("ds1_gjb", getValue(data.get("ifix.scada.DS_JH_TL.DI.DS_JH_TL_P108_RUN.F_CV")));
        result.put("ds2_gjb", getValue(data.get("ifix.scada.DS_JH_TL.DI.DS_JH_TL_P109_RUN.F_CV")));
        result.put("ds_1txscr", getValue(data.get("ifix.scada.DS_JH_TX.AI.DS_JH_TX_T111.F_CV")));
        result.put("ds_2txscr", getValue(data.get("ifix.scada.DS_JH_TX.AI.DS_JH_TX_T121.F_CV")));
        result.put("ds_asb_s", getValue(data.get("ifix.scada.DS_JH_TX.DI.DS_JH_TX_AP001AZO.F_CV")));
        result.put("ds_asb_n", getValue(data.get("ifix.scada.DS_JH_TX.DI.DS_JH_TX_AP001BZO.F_CV")));
        result.put("bsjh_sdv2", getValue(data.get("ifix.scada.SCADA.AI.BS_JH_SD1_GY1_V2.F_CV")));
        result.put("bsjh_sdi2", getValue(data.get("ifix.scada.SCADA.AI.BS_JH_SD1_GY1_I2.F_CV")));
        result.put("bsjh_sd2v2", getValue(data.get("ifix.scada.SCADA.AI.BS_JH_SD2_GY2_V2.F_CV")));
        result.put("bsjh_sd2i2", getValue(data.get("ifix.scada.SCADA.AI.BS_JH_SD2_GY2_I2.F_CV")));
        result.put("data_ph", getValue(data.get("ifix.scada.JIANCE.AI.BS_JH_TL_DATA_BLOCK_1_PH1.F_CV")));
        result.put("bs1_gjb", getValue(data.get("ifix.scada.JIANCE.DI.BS_JH_TL_S5_KM.F_CV")));
        result.put("bs2_gjb", getValue(data.get("ifix.scada.JIANCE.DI.BS_JH_TL_S6_KM.F_CV")));
        result.put("bs1_xhb", getValue(data.get("ifix.scada.JIANCE.AI.BS_JH_TL_DATA_BLOCK_1_S11_DL.F_CV")));
        result.put("bs2_xhb", getValue(data.get("ifix.scada.JIANCE.AI.BS_JH_TL_DATA_BLOCK_1_S12_DL.F_CV")));
        result.put("bs3_xhb", getValue(data.get("ifix.scada.JIANCE.AI.BS_JH_TL_DATA_BLOCK_1_S13_DL.F_CV")));
        result.put("bsyh_fja", getValue(data.get("ifix.scada.JIANCE.AI.BS_JH_TL_DATA_BLOCK_1_S10_DL.F_CV")));
        result.put("bsyh_fjb", getValue(data.get("ifix.scada.JIANCE.AI.BS_JH_TL_DATA_BLOCK_1_S10_1_DL.F_CV")));

        try {
            HktsEnvironmentProtectionDictionaries duanShaoJingHua = new HktsEnvironmentProtectionDictionaries();
            duanShaoJingHua.setId("21");
            duanShaoJingHua.setTableName("duanshaojinghua");
            List<String> jingHuafield = new ArrayList<>();
            jingHuafield.add("xhb_1_A");
            jingHuafield.add("xhb_2_A");
            jingHuafield.add("xhb_3_A");
            jingHuafield.add("xhb_4_A");
            jingHuafield.add("yhfj_1_A");
            jingHuafield.add("yhfj_2_A");

            List<String> jingHuaList = new ArrayList<>();

            jingHuaList.add("xhb_1_A");
            jingHuaList.add("xhb_2_A");
            jingHuaList.add("xhb_3_A");
            jingHuaList.add("xhb_4_A");
            jingHuaList.add("yhfj_1_A");
            jingHuaList.add("yhfj_2_A");
            Map<String, Object> associations = iHktsRequestAddressAssociationService.getAssociations(duanShaoJingHua, jingHuafield,jingHuaList);
            result.put("xhb_1_A", getValue(associations.get("xhb_1_A")));
            result.put("xhb_2_A", getValue(associations.get("xhb_2_A")));
            result.put("xhb_3_A", getValue(associations.get("xhb_3_A")));
            result.put("xhb_4_A", getValue(associations.get("xhb_4_A")));
            result.put("yhfj_1_A", getValue(associations.get("yhfj_1_A")));
            result.put("yhfj_2_A", getValue(associations.get("yhfj_2_A")));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            HktsEnvironmentProtectionDictionaries yanDaoWenDu = new HktsEnvironmentProtectionDictionaries();
            yanDaoWenDu.setId("22");
            yanDaoWenDu.setTableName("yandaowendu");
            List<String> yanDaoField = new ArrayList<>();
            yanDaoField.add("yandao_1_T");
            yanDaoField.add("yandao_1_P");
            yanDaoField.add("yandao_2_T");
            yanDaoField.add("yandao_2_P");
            yanDaoField.add("yandao_3_T");
            yanDaoField.add("yandao_3_P");
            yanDaoField.add("yandao_4_T");
            yanDaoField.add("yandao_4_P");

            List<String> yanDaoList = new ArrayList<>();
            yanDaoList.add("yandao_1_T");
            yanDaoList.add("yandao_1_P");
            yanDaoList.add("yandao_2_T");
            yanDaoList.add("yandao_2_P");
            yanDaoList.add("yandao_3_T");
            yanDaoList.add("yandao_3_P");
            yanDaoList.add("yandao_4_T");
            yanDaoList.add("yandao_4_P");
            Map<String, Object> dSYanDaoWd = iHktsRequestAddressAssociationService.getAssociations(yanDaoWenDu, yanDaoField,yanDaoList);
            result.put("yandao_1_T", getValue(dSYanDaoWd.get("yandao_1_T")));
            result.put("yandao_1_P", getValue(dSYanDaoWd.get("yandao_1_P")));
            result.put("yandao_2_T", getValue(dSYanDaoWd.get("yandao_2_T")));
            result.put("yandao_2_P", getValue(dSYanDaoWd.get("yandao_2_P")));
            result.put("yandao_3_T", getValue(dSYanDaoWd.get("yandao_3_T")));
            result.put("yandao_3_P", getValue(dSYanDaoWd.get("yandao_3_P")));
            result.put("yandao_4_T", getValue(dSYanDaoWd.get("yandao_4_T")));
            result.put("yandao_4_P", getValue(dSYanDaoWd.get("yandao_4_P")));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            HktsEnvironmentProtectionDictionaries duanShaoZhuKong = new HktsEnvironmentProtectionDictionaries();
            duanShaoZhuKong.setId("23");
            duanShaoZhuKong.setTableName("duanshaozhukong3");
            List<String> zhuKong = new ArrayList<>();
            zhuKong.add("dxplccfj_A");
            zhuKong.add("xxplccfj_A");
            List<String> zhuKongList = new ArrayList<>();
            zhuKongList.add("dxplccfj_A");
            zhuKongList.add("xxplccfj_A");
            Map<String, Object> dSZhuKong = iHktsRequestAddressAssociationService.getAssociations(duanShaoZhuKong, zhuKong,zhuKongList);
            result.put("dxplccfj_A", getValue(dSZhuKong.get("dxplccfj_A")));
            result.put("xxplccfj_A", getValue(dSZhuKong.get("xxplccfj_A")));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            HktsEnvironmentProtectionDictionaries peiDian = new HktsEnvironmentProtectionDictionaries();
            peiDian.setId("24");
            peiDian.setTableName("beishao1peidianshi");
            List<String> peiDianField = new ArrayList<>();
            peiDianField.add("dsgyfj_1_A");
            peiDianField.add("dsgyfj_1_V");
            peiDianField.add("dsgyfj_2_A");
            peiDianField.add("dsgyfj_2_V");
            peiDianField.add("bsgyfj_1_A");
            peiDianField.add("bsgyfj_1_V");
            peiDianField.add("bsgyfj_2_A");
            peiDianField.add("bsgyfj_2_V");
            List<String> peiDianList = new ArrayList<>();
            peiDianList.add("dsgyfj_1_A");
            peiDianList.add("dsgyfj_1_V");
            peiDianList.add("dsgyfj_2_A");
            peiDianList.add("dsgyfj_2_V");
            peiDianList.add("bsgyfj_1_A");
            peiDianList.add("bsgyfj_1_V");
            peiDianList.add("bsgyfj_2_A");
            peiDianList.add("bsgyfj_2_V");
            Map<String, Object> bSPeiDian = iHktsRequestAddressAssociationService.getAssociations(peiDian, peiDianField,peiDianList);
            result.put("dsgyfj_1_A", getValue(bSPeiDian.get("dsgyfj_1_A")));
            result.put("dsgyfj_1_V", getValue(bSPeiDian.get("dsgyfj_1_V")));
            result.put("dsgyfj_2_A", getValue(bSPeiDian.get("dsgyfj_2_A")));
            result.put("dsgyfj_2_V", getValue(bSPeiDian.get("dsgyfj_2_V")));
            result.put("bsgyfj_1_A", getValue(bSPeiDian.get("bsgyfj_1_A")));
            result.put("bsgyfj_1_V", getValue(bSPeiDian.get("bsgyfj_1_V")));
            result.put("bsgyfj_2_A", getValue(bSPeiDian.get("bsgyfj_2_A")));
            result.put("bsgyfj_2_V", getValue(bSPeiDian.get("bsgyfj_2_V")));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public float getValue(Object value) {
        if (value != null && !value.toString().equals("null")) {
            return Float.parseFloat(value.toString());
        } else {
            return 0;
        }
    }
    public BigInteger getIntValue(Object value) {
        if (value != null && !value.toString().equals("null")) {
            BigInteger db_1_fwd = new BigInteger(value.toString());
            return db_1_fwd;
        } else {
            BigInteger db_1_fwd = new BigInteger("0");
            return db_1_fwd;
        }
    }
    public Map<String, Object> powerHttpData() {
        Map<String, Object> result = new HashMap<String, Object>();
        HktsEnvironmentProtectionDictionaries peiDian = new HktsEnvironmentProtectionDictionaries();
        peiDian.setId("24");
        peiDian.setTableName("beishao1peidianshi");
        List<String> peiDianField = new ArrayList<>();
        peiDianField.add("db_1_fwd");
        peiDianField.add("db_2_fwd");
        peiDianField.add("sb_1_fwd");
        peiDianField.add("sb_2_fwd");
        peiDianField.add("qb_1_bk");
        peiDianField.add("qb_2_bk");
        List<String> peiDianList = new ArrayList<>();
        peiDianList.add("db_1_fwd");
        peiDianList.add("db_2_fwd");
        peiDianList.add("sb_1_fwd");
        peiDianList.add("sb_2_fwd");
        peiDianList.add("qb_1_bk");
        peiDianList.add("qb_2_bk");
        Map<String, Object> bSPeiDian = iHktsRequestAddressAssociationService.getAssociations(peiDian, peiDianField,peiDianList);

        result.put("db_1_fwd", getIntValue(bSPeiDian.get("db_1_fwd")));
        result.put("db_2_fwd", getIntValue(bSPeiDian.get("db_2_fwd")));
        result.put("sb_1_fwd", getValue(bSPeiDian.get("sb_1_fwd")));
        result.put("sb_2_fwd", getValue(bSPeiDian.get("sb_2_fwd")));
        result.put("qb_1_bk", getValue(bSPeiDian.get("qb_1_bk")));
        result.put("qb_2_bk", getIntValue(bSPeiDian.get("qb_2_bk")));
        return result;
    }

    public Map<String, Object> baijingDataInterface() throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("请求时间：{}", format.format(new Date()));
        //TODO 判断当前的运行状态
        Map<String, Object> result = new HashMap<String, Object>();
        String s = httpAPIService.doGet("http://192.168.99.67:5000//biz");
        Gson gson = new Gson();
        Map map = new HashMap();
        map = gson.fromJson(s, map.getClass());
        Map data = (Map) map.get("data");

        result.put("ds_1sd_2v", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD2_ADONG_CI_DIAN_YA.F_CV")));
        result.put("ds_1sd_2i", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD2_YI_DIAN_CD_CI_DL.F_CV")));
        result.put("ds_2sd_2v", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD3_BDONG_CI_DIAN_YA.F_CV")));
        result.put("ds_2sd_2i", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_SD3_DONG_DIAN_CD_CI_DL.F_CV")));
        result.put("dsjh_tlph", getValue(data.get("ifix.scada.DS_JH_TL.AI.DS_JH_TL_PH_101.F_CV")));
        result.put("ds1_gjb", getValue(data.get("ifix.scada.DS_JH_TL.DI.DS_JH_TL_P108_RUN.F_CV")));
        result.put("ds2_gjb", getValue(data.get("ifix.scada.DS_JH_TL.DI.DS_JH_TL_P109_RUN.F_CV")));

        try {
            HktsEnvironmentProtectionDictionaries duanShaoJingHua = new HktsEnvironmentProtectionDictionaries();
            duanShaoJingHua.setId("21");
            duanShaoJingHua.setTableName("duanshaojinghua");
            List<String> jingHuafield = new ArrayList<>();
            jingHuafield.add("xhb_1_A");
            jingHuafield.add("xhb_2_A");
            jingHuafield.add("xhb_3_A");
            jingHuafield.add("xhb_4_A");
            jingHuafield.add("yhfj_1_A");
            jingHuafield.add("yhfj_2_A");
            List<String> jingHuaList = new ArrayList<>();
            jingHuaList.add("xhb_1_A");
            jingHuaList.add("xhb_2_A");
            jingHuaList.add("xhb_3_A");
            jingHuaList.add("xhb_4_A");
            jingHuaList.add("yhfj_1_A");
            jingHuaList.add("yhfj_2_A");
            Map<String, Object> associations = iHktsRequestAddressAssociationService.getAssociations(duanShaoJingHua, jingHuafield,jingHuaList);
            result.put("xhb_1_A", getValue(associations.get("xhb_1_A")));
            result.put("xhb_2_A", getValue(associations.get("xhb_2_A")));
            result.put("xhb_3_A", getValue(associations.get("xhb_3_A")));
            result.put("xhb_4_A", getValue(associations.get("xhb_4_A")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HktsEnvironmentProtectionDictionaries yanDaoWenDu = new HktsEnvironmentProtectionDictionaries();
            yanDaoWenDu.setId("22");
            yanDaoWenDu.setTableName("yandaowendu");
            List<String> yanDaoField = new ArrayList<>();
            yanDaoField.add("yandao_1_T");
            yanDaoField.add("yandao_1_P");
            yanDaoField.add("yandao_2_T");
            yanDaoField.add("yandao_2_P");
            yanDaoField.add("yandao_3_T");
            yanDaoField.add("yandao_3_P");
            yanDaoField.add("yandao_4_T");
            yanDaoField.add("yandao_4_P");
            List<String> yanDaoList = new ArrayList<>();
            yanDaoList.add("yandao_1_T");
            yanDaoList.add("yandao_1_P");
            yanDaoList.add("yandao_2_T");
            yanDaoList.add("yandao_2_P");
            yanDaoList.add("yandao_3_T");
            yanDaoList.add("yandao_3_P");
            yanDaoList.add("yandao_4_T");
            yanDaoList.add("yandao_4_P");

            Map<String, Object> dSYanDaoWd = iHktsRequestAddressAssociationService.getAssociations(yanDaoWenDu, yanDaoField,yanDaoList);
            result.put("yandao_1_T", getValue(dSYanDaoWd.get("yandao_1_T")));
            //result.put("yandao_1_P", getValue(dSYanDaoWd.get("yandao_1_P")));
            result.put("yandao_2_T", getValue(dSYanDaoWd.get("yandao_2_T")));
            //result.put("yandao_2_P", getValue(dSYanDaoWd.get("yandao_2_P")));
            result.put("yandao_3_T", getValue(dSYanDaoWd.get("yandao_3_T")));
            //result.put("yandao_3_P", getValue(dSYanDaoWd.get("yandao_3_P")));
            result.put("yandao_4_T", getValue(dSYanDaoWd.get("yandao_4_T")));
            //result.put("yandao_4_P", getValue(dSYanDaoWd.get("yandao_4_P")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            HktsEnvironmentProtectionDictionaries peiDian = new HktsEnvironmentProtectionDictionaries();
            peiDian.setId("24");
            peiDian.setTableName("beishao1peidianshi");
            List<String> peiDianField = new ArrayList<>();
            peiDianField.add("dsgyfj_1_A");
            peiDianField.add("dsgyfj_1_V");
            peiDianField.add("dsgyfj_2_A");
            peiDianField.add("dsgyfj_2_V");
            peiDianField.add("bsgyfj_1_A");
            peiDianField.add("bsgyfj_1_V");
            peiDianField.add("bsgyfj_2_A");
            peiDianField.add("bsgyfj_2_V");
            List<String> peiDianList = new ArrayList<>();
            peiDianList.add("dsgyfj_1_A");
            peiDianList.add("dsgyfj_1_V");
            peiDianList.add("dsgyfj_2_A");
            peiDianList.add("dsgyfj_2_V");
            peiDianList.add("bsgyfj_1_A");
            peiDianList.add("bsgyfj_1_V");
            peiDianList.add("bsgyfj_2_A");
            peiDianList.add("bsgyfj_2_V");
            Map<String, Object> bSPeiDian = iHktsRequestAddressAssociationService.getAssociations(peiDian, peiDianField,peiDianList);
            result.put("dsgyfj_1_A", getValue(bSPeiDian.get("dsgyfj_1_A")));
            result.put("dsgyfj_2_A", getValue(bSPeiDian.get("dsgyfj_2_A")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
